package com.ibeeking.found.msg.rest.service.impl;

import com.ibeeking.found.msg.rest.service.IMpAutoMessageKeyService;
import com.ibeeking.found.msg.service.common.bo.MpAutoMessageKeyBO;
import com.ibeeking.found.msg.service.common.dos.MpAutoMessageKeyDO;
import com.ibeeking.found.msg.service.common.query.KeyWordQuery;
import com.ibeeking.found.msg.service.mapper.MpAutoMessageKeyMapper;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MpAutoMessageKeyServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-02-07 11:30
 **/
@Service
public class MpAutoMessageKeyServiceImpl implements IMpAutoMessageKeyService {

    @Resource
    private MpAutoMessageKeyMapper mpAutoMessageKeyMapper;

    @Override
    public Boolean modify(Long messageId, String appId, List<String> keys) {
        mpAutoMessageKeyMapper.delete(new LambdaQueryWrapper<>(MpAutoMessageKeyDO.class).eq(MpAutoMessageKeyDO::getMessageId, messageId));
        List<MpAutoMessageKeyDO> saves = new ArrayList<>();
        keys.stream().forEach(key -> {
            MpAutoMessageKeyDO keyDO = new MpAutoMessageKeyDO();
            keyDO.setAppId(appId);
            keyDO.setMessageId(messageId);
            keyDO.setKeyword(key);
            keyDO.setAllEquals(0);
            keyDO.setCreateTime(LocalDateTime.now());
            saves.add(keyDO);
        });
        return mpAutoMessageKeyMapper.insertBatchSomeColumn(saves) > 0;
    }

    @Override
    public Boolean deleteByMessageId(Long messageId) {
        return mpAutoMessageKeyMapper.delete(new LambdaQueryWrapper<>(MpAutoMessageKeyDO.class).eq(MpAutoMessageKeyDO::getMessageId, messageId)) > 0;
    }

    @Override
    public List<MpAutoMessageKeyBO> queryByMessageId(Long messageId) {
        List<MpAutoMessageKeyDO> mpAutoMessageKeyDos = mpAutoMessageKeyMapper.selectList(new LambdaQueryWrapper<>(MpAutoMessageKeyDO.class).eq(MpAutoMessageKeyDO::getMessageId, messageId));
        return JsonUtils.jsonToList(mpAutoMessageKeyDos, MpAutoMessageKeyBO.class);
    }

    @Override
    public List<MpAutoMessageKeyBO> queryByKeyWord(KeyWordQuery query, boolean allEquals) {
        LambdaQueryWrapper<MpAutoMessageKeyDO> queryWrapper = new LambdaQueryWrapper<>(MpAutoMessageKeyDO.class)
                .eq(MpAutoMessageKeyDO::getAppId, query.getAppId())
                .orderByAsc(MpAutoMessageKeyDO::getCreateTime);
        if (allEquals) {
            queryWrapper.eq(MpAutoMessageKeyDO::getKeyword, query.getKeyWord());
        } else {
            queryWrapper.like(MpAutoMessageKeyDO::getKeyword, query.getKeyWord());
        }
        List<MpAutoMessageKeyDO> mpAutoMessageKeyDos = mpAutoMessageKeyMapper.selectList(queryWrapper);
        return JsonUtils.jsonToList(mpAutoMessageKeyDos, MpAutoMessageKeyBO.class);
    }
}
